"""
@Project:newpro
@File:websocket_anli.py
@Author:毛敬龙
@Date:9:55
"""
import json

import tornado.websocket
conndict={}
userlist=[]
class WebsockHandler(tornado.websocket.WebSocketHandler):
    # 跨域支持
    def check_origin(self, origin):
        return True
    #建立连接
    def open(self):
        #获取参数
        id = int(self.get_query_argument("id"))
        name = self.get_query_argument("name")
        #存储链接
        conndict[id]=self
        # if str(name)=='客服':
        #     self.write_message({'code':200,'msg':'成功','data':userlist})
    #接收消息
    def on_message(self,mes):
        print(mes)
        mes=json.loads(mes)
        id=int(mes['id'])
        mes=mes['mes']
        print(mes)
        conndict[id].write_message({'code':200,'msg':'成功','data':mes})

    def on_close(self):
        pass

import tornado.web
class TTHandler(tornado.web.RequestHandler):
    def get(self):
        msg = self.get_query_argument("mes")
        id = int(self.get_query_argument("id"))
        conndict[id].write_message(msg)
        # 根据id返回对应客户信息，其价值和意义


        # while True:   # 失败，无法实时获取最新信息
        #     time.sleep(15)
        #     sql = 'select * from greenhouse'
        #     res = dbs.find_all(sql)
        #     lis = []
        #     for i in res:
        #         if int(i['heat_min'])<int(i['heat'])<int(i['heat_max']):
        #             status=1
        #         else:
        #             status=2
        #         dat={
        #             'id':i['id'],
        #             'name':i['name'],
        #             'heat':i['heat'],
        #             'status':status
        #         }
        #         lis.append(dat)
        #     conndict[id].write_message({'code':200,'msg':'成功','data':lis})
        #     print(1)


import time
from utils.my_redis import r
from init_db import dbs
class HouseHandler(tornado.web.RequestHandler):
    def get(self):
        id = 2
        while True:
            key1 = r.get_str('house_1')
            if key1:
                key1 = int(key1.decode())
                sql = 'select * from greenhouse where id=4'
                res = dbs.findone(sql)
                status = 1
                if key1 < int(res['heat_min']) or key1 > int(res['heat_max']):
                    status = 2
                conndict[id].write_message({'code': 200, 'msg': '返回', 'heat': key1, 'status': status})
            time.sleep(10)


# 模拟标的审核实时获取信息
# 首先分配标的 开始时审核员获取自身未审核的全部信息 之后如果有信息加入就通过conndict[id].write_message 传输
# 然后前端 push添加列表 显示

# 首先是两个人物：客服和客户 先拟定都在前端写
# 他们都有对话框，发的消息都能显示，双方都能看到，前端的列表存储两个人的信息

class KefuHandler(tornado.web.RequestHandler):
    def get(self):
        pass




# 你所愿的终将离去，你所拥有的也将失去，但代价是你会体验新的、从未拥有的事物







